package com.engine.salary.wrapper;

import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.salary.entity.datacollection.AddUpDeduction;
import com.engine.salary.entity.datacollection.dto.AddUpDeductionDTO;
import com.engine.salary.entity.datacollection.dto.AddUpDeductionRecordDTO;
import com.engine.salary.entity.datacollection.param.*;
import com.engine.salary.exception.SalaryRunTimeException;
import com.engine.salary.service.AddUpDeductionService;
import com.engine.salary.service.SalaryEmployeeService;
import com.engine.salary.service.TaxAgentService;
import com.engine.salary.service.impl.AddUpDeductionServiceImpl;
import com.engine.salary.service.impl.SalaryEmployeeServiceImpl;
import com.engine.salary.service.impl.TaxAgentServiceImpl;
import com.engine.salary.util.SalaryDateUtil;
import com.engine.salary.util.SalaryI18nUtil;
import com.engine.salary.util.page.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import weaver.general.BaseBean;
import weaver.hrm.User;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 累计专项附加扣除
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
@Slf4j
public class AddUpDeductionWrapper extends Service {

    private final BaseBean baseBean = new BaseBean();
    private final Boolean isLog = "true".equals(baseBean.getPropValue("hrmSalary", "log"));

    private AddUpDeductionService getAddUpDeductionService(User user) {
        return ServiceUtil.getService(AddUpDeductionServiceImpl.class, user);
    }

    private TaxAgentService getTaxAgentService(User user) {
        return ServiceUtil.getService(TaxAgentServiceImpl.class, user);
    }

    private SalaryEmployeeService getSalaryEmployeeService(User user) {
        return ServiceUtil.getService(SalaryEmployeeServiceImpl.class, user);
    }


    /**
     * 数据采集-累计专项附加扣除列表(分页)
     *
     * @param queryParam
     * @return
     */
    public PageInfo<AddUpDeductionDTO> list(AddUpDeductionQueryParam queryParam) {
        PageInfo<AddUpDeductionDTO> pageInfo = getAddUpDeductionService(user).listPage(queryParam);

        return pageInfo;
    }

    /**
     * 数据采集-累计专项附加扣除详情列表(分页)
     *
     * @param queryParam
     * @return
     */
    public PageInfo<AddUpDeductionRecordDTO> getDetailList(AddUpDeductionQueryParam queryParam) {
        Long id = queryParam.getAccumulatedSpecialAdditionalDeductionId();
        AddUpDeduction po = getAddUpDeductionService(user).getById(id);
        if (po == null) {
            throw new SalaryRunTimeException(String.format(SalaryI18nUtil.getI18nLabel(100338, "累计专项附加扣除不存在") + "[id:%s]", id));
        }

        //申报月份
        List<String> declareMonth = queryParam.getDeclareMonth();
        if (CollectionUtils.isNotEmpty(declareMonth)) {
            queryParam.setDeclareMonth(declareMonth.stream().map(e -> e + "-01 00:00:00").collect(Collectors.toList()));
            queryParam.setDeclareMonthDate(declareMonth.stream().map(e -> e + "-01 00:00:00").map(SalaryDateUtil::dateStrToLocalTime).collect(Collectors.toList()));
        }
        queryParam.setEmployeeId(po.getEmployeeId());

        PageInfo<AddUpDeductionRecordDTO> page = getAddUpDeductionService(user).recordListPage(queryParam);

        return page;
    }


    /**
     * 导出-累计专项附加扣除列表
     *
     * @param queryParam
     * @return
     */
    public XSSFWorkbook export(AddUpDeductionQueryParam queryParam) {
        return getAddUpDeductionService(user).export( queryParam);
    }

    /**
     * 导出-累计专项附加扣除详情列表
     *
     * @param queryParam
     * @return
     */
    public XSSFWorkbook exportDetail(AddUpDeductionQueryParam queryParam) {
        Long id = queryParam.getAccumulatedSpecialAdditionalDeductionId();
        AddUpDeduction po = getAddUpDeductionService(user).getById(id);
        if (po == null) {
            throw new SalaryRunTimeException(String.format(SalaryI18nUtil.getI18nLabel(100338, "累计专项附加扣除不存在") + "[id:%s]", id));
        }

        return getAddUpDeductionService(user).exportDetail(po.getEmployeeId(), queryParam);
    }

    /**
     * 下载导入模板
     *
     * @param queryParam
     * @return
     */
    public XSSFWorkbook downloadTemplate(AddUpDeductionQueryParam queryParam) {
        return getAddUpDeductionService(user).downloadTemplate(queryParam);
    }


    public Map<String, Object> importAddUpDeduction(AddUpDeductionImportParam importParam) {
        return getAddUpDeductionService(user).importAddUpDeduction(importParam);
    }


    public Map<String, Object> preview(AddUpDeductionImportParam importParam) {
        return getAddUpDeductionService(user).preview(importParam);
    }

    public Map<String, Object> getSearchCondition(Map<String, Object> params) {
        return getAddUpDeductionService(user).getSearchCondition(params);
    }

    public void editAddUpDeduction(AddUpDeductionRecordParam addUpDeduction) {
        getAddUpDeductionService(user).editAddUpDeduction(addUpDeduction);
    }

    public void createAddUpDeduction(AddUpDeductionRecordParam addUpDeductionRecordParam) {
        getAddUpDeductionService(user).createAddUpDeduction(addUpDeductionRecordParam);
    }

    public void deleteSelectAddUpDeduction(AddUpDeductionRecordDeleteParam deleteParam) {
        getAddUpDeductionService(user).deleteSelectAddUpDeduction(deleteParam);
    }

    public void deleteAllAddUpDeduction(AddUpDeductionRecordDeleteParam deleteParam) {
        getAddUpDeductionService(user).deleteAllAddUpDeduction(deleteParam);
    }

    public AddUpDeductionRecordDTO getAddUpDeduction(AddUpDeductionQueryParam param) {
        return getAddUpDeductionService(user).getAddUpDeduction(param);
    }

    public String autoAddAll(AddDeductionAutoAddParam param) {
        Date yearMonth = param.getYearMonthDate();
        if (yearMonth == null) {
            throw new SalaryRunTimeException("一键累计传入日期格式错误");
        }
        return getAddUpDeductionService(user).autoAddAll(yearMonth, false, param.getTaxAgentIds());
    }
}
